package com.project.quiktask.controller;

import java.sql.Date;
import java.sql.Time;
import java.util.Comparator;

import com.project.quiktask.Task;
import com.project.quiktask.util.Variable;

public class DeadlineComparator implements Comparator<Task>{
	
    public int compare(Task taskOne, Task taskTwo) {
       
    	Date taskOneDeadlineDate = taskOne.getDeadlineDate();
    	Time taskOneDeadlineTime = taskOne.getDeadlineTime();
    			
    	Date taskTwoDeadlineDate = taskTwo.getDeadlineDate();
    	Time taskTwoDeadlineTime = taskTwo.getDeadlineTime();
    	
    	int result = getMoreUrgentTask(taskOneDeadlineDate, taskTwoDeadlineDate, taskOneDeadlineTime, taskTwoDeadlineTime);
    	
    	return result;
    }
    
    public int getMoreUrgentTask (Date taskOneDeadlineDate, Date taskTwoDeadlineDate, 
    		Time taskOneDeadlineTime, Time taskTwoDeadlineTime) {
    	
    	int result;
    	
    	if (isDateNull(taskOneDeadlineDate) && !isDateNull(taskTwoDeadlineDate))
    		return Variable.SECOND_TASK_MORE_URGENT;
    	else if (!isDateNull(taskOneDeadlineDate) && isDateNull(taskTwoDeadlineDate))
    		return Variable.FIRST_TASK_MORE_URGENT;
    	else {
    		result = taskOneDeadlineDate.compareTo(taskTwoDeadlineDate);
        	if (result != Variable.EQUAL_URGENCY)
        		return result;
        	else {
        		if (isTimeNull(taskOneDeadlineTime) && !isTimeNull(taskTwoDeadlineTime)) 
        			return Variable.SECOND_TASK_MORE_URGENT;
        		else if (!isTimeNull(taskOneDeadlineTime) && isTimeNull(taskTwoDeadlineTime))
            		return Variable.FIRST_TASK_MORE_URGENT;
            	else {
            		result = taskOneDeadlineTime.compareTo(taskTwoDeadlineTime);
            		if (result < Variable.EQUAL_URGENCY)
            			return Variable.FIRST_TASK_MORE_URGENT;
            		else
            			return Variable.SECOND_TASK_MORE_URGENT;
        		}
        	}
    	}
    }
    
    public boolean isDateNull(Date date) {
    	if (date == null)
    		return true;
    	else 
    		return false;
    }
    
    public boolean isTimeNull(Time time) {
    	if (time == null)
    		return true;
    	else
    		return false;
    }
}